﻿using System.ComponentModel.Composition;
using ImageClipTool.Applications.ViewModels;
using System.Waf.Applications;
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;

namespace ImageClipTool.Applications.Controllers
{
    [Export]
    internal class ShellController
    {
        private readonly ImageClipController imageClipController;
        private readonly ShellViewModel shellViewModel;
        private readonly DelegateCommand selectImageCommand;


        [ImportingConstructor]
        public ShellController(ShellViewModel shellViewModel, ImageClipController imageClipController)
        {
            this.imageClipController = imageClipController;
            this.shellViewModel = shellViewModel;
            this.selectImageCommand = new DelegateCommand(SelectImageCommand);
        }

        private void SelectImageCommand()
        {
            Microsoft.Win32.OpenFileDialog dialog = new Microsoft.Win32.OpenFileDialog();
            dialog.Filter = "png;jpg|*.png;*jpg";
            if (dialog.ShowDialog() == true)
            {
                if (dialog.FileName != null && !dialog.FileName.Equals(string.Empty))
                {
                    Image image = new Image();
                    image.Source = new BitmapImage(new Uri(dialog.FileName));
                    imageClipController.Show((BitmapImage)image.Source);
                }
            }
        }

        public void Initialize()
        {
            shellViewModel.SelectImageCommand = this.selectImageCommand;
        }

    }
}
